﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lm.Common.ExtensionMethods;

namespace Lm.Common.Auth.Token
{
    class TokenValidator
    {
        public Model.Token Validate(string token)
        {
            if (token.IsNullOrBlank())
            { throw new Exception("必须输入Token"); }
            var helper = Lm.Common.Auth.DAL.DALFactory.Create();
            using (var tran = new System.Transactions.TransactionScope())
            {
                if (helper.IsSingle<Model.Token>(t => t.UserToken == token) == false)
                { throw new Exception("Token不存在：" + token); }
                var tokenModel = helper.GetSingle<Model.Token>(t => t.UserToken == token);
                if (tokenModel.ExpireTime < DateTime.Now)
                { throw new Exception("Token已过期：" + token + "," + tokenModel.ExpireTime); }
                tran.Complete();
                return tokenModel;
            }
        }
    }
}
